﻿package scripts
{
	import flash.geom.Point;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	
	import fl.motion.easing.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.display.Shape;
	
	
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	import flash.system.Security;
	import flash.text.TextField;
	import com.greensock.TweenLite;

	public class AXA extends MovieClip
	{
		const XML_URL:String = "asset/AXA.xml";
		static var _myXmlLoader:XMLLoader;
		static var _myXML:XML;
		protected var initpage;
		protected var totalpage:Number;
		
		//protected var listpage:Number;
		protected var b_width:Number;
		protected var b_height:Number;
		protected var stage_width:Number;
		protected var stage_height:Number;
		protected var array_list:Array;
		protected var pos_root:Point;
		protected var mypageflip:PageFlipClass;
		protected var book_container:BookContainer;
		
		
		
		
		//for zooming use
		private var oldX:Number;
		private var oldY:Number;
		private var oldMouseX:Number;
		private var oldMouseY:Number;
		private var dragStart:Boolean;
		private var cont_w:Number;
		private var cont_h:Number;
		//zoom end
		
		
		private var stageInfo:ClickableText;

		public function AXA()
		{
			//Security.allowDomain("s.ytimg.com","www.youtube.com","s0.2mdn.net");
			Security.allowDomain("*");
			
			mypageflip=new PageFlipClass();
			//mypageflip.onLoading = onLoading;
			mypageflip.onLoadEnd = onLoadEnd;
			//mypageflip.onPageEnd = onPageEnd;
			mypageflip.downHandler = downHandler;
			mypageflip.upHandler = upHandler;
			mypageflip.mystage=stage;
			// constructor code
			
			_myXmlLoader = new XMLLoader(this.getAbsolutePath() + XML_URL);
			//trace(this.getAbsolutePath()+XML_URL);
			_myXmlLoader.addEventListener(Event.COMPLETE,xmlComplete);
		}
		protected function init():void
		{
			
			initpage = 0;
			
			//listpage = 0;
			b_width = _myXML.@width;
			b_height = _myXML.@height;
			stage_width = b_width * 2;
			stage_height = b_height * 2;
			array_list=new Array();
			pos_root = new Point(0,0);
			/*
			var tmplen:Number=(((totalpage)/20)>>0)+1;
			var endpage:Number=(((totalpage)/2)>>0)+1;
			var Td:Number = 10;
			for (var i:Number=0; i<tmplen; i++)
			{
				var tmpn1:Number=(initpage+1)+i*Td;
				var tmpn2:Number=tmpn1+(Td-1)>endpage?endpage:tmpn1+(Td-1);
				array_list.push({n1:tmpn1,n2:tmpn2});
			}*/
			//pageNumber(array_list[listpage]["n1"],array_list[listpage]["n2"]);
			//trace(tmplen,totalpage,endpage,tmpn2);
			stage.addEventListener(Event.RESIZE, resizeHandler);
			
			
		}
		protected function initContainer():void
		{
			book_container=new BookContainer();
			book_container.name="container";
			
			book_container.x=pos_root.x+(stage_width-2*b_width)*0.3;
			book_container.y=0;
			addChild(book_container);
			var book_Mask:Shape=new Shape();
			book_Mask.graphics.beginFill(0xff0000,0.5);
			book_Mask.graphics.drawRect(0,0,stage_width,stage_height);
			book_Mask.graphics.endFill();
			addChild(book_Mask);
			book_container.mask=book_Mask;
			
			stageInfo=new ClickableText("",false,"",500);
			stageInfo.setFontSize(20);
			stageInfo.setColour(0xFF0000);
			stageInfo.setHtmlText(stage.stageWidth+","+stage.stageHeight);
			//stageInfo.visible=false;
			addChild(stageInfo);
			mypageflip.output=stageInfo;
			//this.swapChildren(book_container,output);
			mypageflip._xmlTotalPage=_myXML.pages.children().length();
			mypageflip.book_totalpage = this.totalpage = 2*(mypageflip._xmlTotalPage-1);
			
			//trace(mypageflip.book_totalpage);
			mypageflip.book_totalpage = totalpage;
			mypageflip.book_root = book_container.book;
			mypageflip.book_initpage = initpage;
			mypageflip.book_TimerNum = 20;
			mypageflip.InitBook();

			
		}
		/*
		*  XML file loaded and store at parameter _myXML.
		*/
		protected function xmlComplete(e:Event):void
		{
			
			_myXmlLoader.removeEventListener(Event.COMPLETE,xmlComplete);
			mypageflip.myXML=_myXML=_myXmlLoader.getXML();
			init();
			initContainer();
		}

		/*
		*Get the absolute path of the swf file, it will kill the relative path caused XML file could not found error;
		*/
		protected function getAbsolutePath():String
		{
			
			var swfURL:String = loaderInfo.url;
			return swfURL.substr(0, swfURL.lastIndexOf("/") + 1);
		}
		function resizeHandler(event:Event):void
		{
			pos_root.x-=(stage.stageWidth-stage_width)*0.5;
			pos_root.y-=(stage.stageHeight-stage_height)*0.5;
			stage_width = stage.stageWidth;
			stage_height = stage.stageHeight;
		
			book_container.x=pos_root.x+(stage_width-2*b_width)*0.5;
			trace(book_container.x);
			book_container.y=pos_root.y+((stage_height-50)-b_height)*0.5;
			
		}



		protected function onLoading(pageMC:MovieClip,loadedPercent:Number)
		{
			pageMC["loadmc"]["pText"].text = loadedPercent;
		}
		protected function onLoadEnd()
		{
			addlistener();
			
		}
		/*
		* 	Zooming functions start
		*/

		private function addlistener():void
		{
			book_container.scrollRect = new Rectangle(0,0,stage_width,stage_height);
			book_container.book.addEventListener(MouseEvent.MOUSE_DOWN,downHandler,true);
			book_container.book.addEventListener(MouseEvent.MOUSE_UP,upHandler,true);
			stage.addEventListener("GO_TO_PAGE",goToHandler,true);
			book_container.scaleX = book_container.scaleY =1; //2;//
		}
		protected function downHandler(evt:MouseEvent):void
		{
			
			
				if(dragStart){
					stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
				}
				
				oldX = book_container.scrollRect.x;
				oldY = book_container.scrollRect.y;
				oldMouseX = evt.stageX;
				oldMouseY = evt.stageY;
			
		}
		
		protected function moveHandler(evt:MouseEvent):void
		{
			
			const disX:Number = oldMouseX - evt.stageX;
			const disY:Number = oldMouseY - evt.stageY;
			const rect:Rectangle = book_container.scrollRect;
			rect.x = oldX + disX;
			rect.y = oldY + disY;
			book_container.scrollRect = rect;
			
		}
		
		protected function upHandler(evt:MouseEvent):void
		{
			zoomInOrOut(evt);

		}
		protected function zoomInOrOut(evt:MouseEvent):void{
			if((oldMouseX == evt.stageX) && (oldMouseY == evt.stageY)){
					
					dragStart = !dragStart;
					if(mypageflip.book_page==0||mypageflip.book_page!=mypageflip.book_totalpage){
						
						var tw:Number = 2400*(Math.ceil(2200/stage.stageWidth));
						var th:Number = 2000*(Math.ceil(2200/stage.stageWidth));
						book_container.height=(book_container.height!=2400)?2400:(2400*((2000/stage.stageWidth)));
						book_container.width=(book_container.width!=2000)?2000:(2000*((2000/stage.stageWidth)));
						book_container.scrollRect = new Rectangle(0,0,stage_width,stage_height);
						stageInfo.setHtmlText(book_container.width+","+book_container.height);
						/*if(book_container.width!=2000){
							var rect:Rectangle = book_container.scrollRect;
    						rect.x += stage_width*0.25;
   							book_container.scrollRect = rect;
						}*/
					}
					else{
						book_container.height=(book_container.height!=2400)?2400:2400*(2000/stage.stageWidth);
						book_container.width=(book_container.width!=2000)?2000:2000*(2000/stage.stageWidth);
						book_container.scrollRect = new Rectangle(0,0,stage_width,stage_height);
					}
					
			}
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
		}
		protected function goToHandler(e:Event):void{
			//stageInfo.setHtmlText("Go to Page: "+e.target.page_number);
			mypageflip.PageGoto(e.target.page_number);
		}
		
		
	}

}